﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.Orgs;
using SmartSpace.Web;

namespace SmartSpace.WebApp.Modules.Orgs.Pages
{
    public partial class Node_Edit : SsTemplatePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String nodeId = Request.QueryString["nodeID"] ?? String.Empty;
            if (!IsPostBack)
            {
                BindSelect();
                BindNode(nodeId);
            }
        }

        public void BindSelect()
        {
            OrgsService services = new OrgsService();
            ListResult<NodeType> list = services.GetTypes();

            ddl_nodeType.DataSource = list.Data;
            ddl_nodeType.DataTextField = "Caption";
            ddl_nodeType.DataValueField = "Id";
            ddl_nodeType.DataBind();
        }

        public void BindNode(string nodeId)
        {
            OrgsService services = new OrgsService();
            Node node = services.GetNode(new Guid(nodeId), true);
            org_tree_code.Text = node.Code;
            org_tree_name.Text = node.Name;
            ddl_nodeType.SelectedValue = node.TypeId.ToString();
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            Node node = new Node();
            node.Id = new Guid(Request.QueryString["nodeID"] ?? String.Empty);
            node.Code = this.org_tree_code.Text;
            node.Name = this.org_tree_name.Text;
            node.TypeId = new Guid(this.ddl_nodeType.SelectedItem.Value);
            OrgsService services = new OrgsService();

            bool bl = services.SaveNode(node);
            if (bl)
            {
                Response.Redirect("OrgTree.aspx");
            }
        }

    }
}